
'''
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
'''

'''
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.
    
    To request the provided software under a different license you can contact us at
    support@xoronos.com
'''

#####################################################
##                   Introduction                  ##
#####################################################
## This code example shows how to compute the      ##
## monomial key exchange.                          ##
#####################################################

#####################################################
##                   Pre-requisits                 ##
#####################################################
## The Xoron matrix decompressed data structure    ##
## needs to be mined before running this code      ##
## example. Some examples on how to mine Xoron     ##
## matrices can be found in                        ##
## generate_xrn_matrix_all_rnd.py or               ##
## generate_xrn_matrix_one_third.py                ##
##                                                 ##
## Two monomial keys.                              ##
## Two binomial keys.                              ##
##                                                 ##
## An example on how to generate keys can be found ##
## in generate_keys_with_wrapper_functions.py      ##
#####################################################

import xrnlib256 as xrn

def generate_keys_alice_bob ( ) :

    # library run time settings
    settings = xrn.xrn_crypto_extra_settings_t ()
    # all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    #############################
    ## Variable initialization ##
    #############################

    # Initialize default run time settings
    # In this code example only the settings.rnd_str is used
    # In the xrn_load_default_settings function xrn_make_start_point string is used.
    # Other functions names can be also used to initialize settings

    if ( xrn.XSUCCESS != xrn.xrn_load_default_settings( settings ) ) :
        return -1

    #################
    ## Start point ##
    #################

    print( "making start point ..." );

    start_point_fp = xrn.fopen( "start_point.xf", "wb" );
    decompressed_xrn_matrix_fp = xrn.fopen( "xrn_matrix.xm", "rb" );
    if (( start_point_fp == None ) or ( decompressed_xrn_matrix_fp == None ) ):
        return -1

    if ( xrn.XSUCCESS != xrn.xrn_make_start_point_wrapper( start_point_fp, decompressed_xrn_matrix_fp, settings ) ) :
        xrn.fclose( decompressed_xrn_matrix_fp )
        xrn.fclose( start_point_fp )
        return -1

    if (( 0 != xrn.fclose( start_point_fp ) ) or ( 0 != xrn.fclose( decompressed_xrn_matrix_fp ) ) ):
        return -1

    #########################
    ## Monomial keys point ##
    #########################

    print( "making monomial keys ..." )

    alice_monomial_key_fp = xrn.fopen( "alice_monomial_key.xf", "wb" );
    if ( alice_monomial_key_fp == None ) :
        return -1

    if ( xrn.XSUCCESS != xrn.xrn_make_monomial_key_wrapper( alice_monomial_key_fp, settings ) ) :
        xrn.fclose( alice_monomial_key_fp )
        return -1

    if ( 0 != xrn.fclose( alice_monomial_key_fp ) ) :
        return -1

    bob_monomial_key_fp = xrn.fopen( "bob_monomial_key.xf", "wb" )
    if ( bob_monomial_key_fp == None ) :
        return -1

    if ( xrn.XSUCCESS != xrn.xrn_make_monomial_key_wrapper( bob_monomial_key_fp, settings ) ) :
        xrn.fclose( bob_monomial_key_fp )
        return -1

    if ( 0 != xrn.fclose( bob_monomial_key_fp ) ) :
        return -1

    #########################
    ## Binomial key Alice  ##
    #########################

    print( "making alice binomial keys ..." )

    start_point_fp = xrn.fopen( "start_point.xf", "rb" )
    decompressed_xrn_matrix_fp = xrn.fopen( "xrn_matrix.xm", "rb" )
    alice_monomial_key_fp = xrn.fopen( "alice_monomial_key.xf", "rb" )
    alice_binomial_key_fp = xrn.fopen( "alice_binomial_key.xf", "wb" )

    if (( start_point_fp == None ) or 
        ( decompressed_xrn_matrix_fp == None ) or 
        ( alice_monomial_key_fp == None ) or 
        ( alice_binomial_key_fp == None ) ):
        return -1

    if ( xrn.XSUCCESS !=
         xrn.xrn_make_binomial_key_wrapper( decompressed_xrn_matrix_fp, start_point_fp, alice_monomial_key_fp,
                                         alice_binomial_key_fp, settings ) ) :
        xrn.fclose( alice_binomial_key_fp )
        xrn.fclose( decompressed_xrn_matrix_fp )
        xrn.fclose( alice_monomial_key_fp )
        xrn.fclose( start_point_fp )
        return -1

    if ( ( 0 != xrn.fclose( alice_monomial_key_fp ) ) or ( 0 != xrn.fclose( alice_binomial_key_fp ) ) ) :
        return -1

    xrn.rewind( decompressed_xrn_matrix_fp )
    xrn.rewind( start_point_fp )

    ######################
    ## Binomial key Bob ##
    ######################

    print( "making bob binomial keys ..." )

    bob_monomial_key_fp = xrn.fopen( "bob_monomial_key.xf", "rb" )
    bob_binomial_key_fp = xrn.fopen( "bob_binomial_key.xf", "wb" )
    if (( bob_binomial_key_fp == None ) or ( bob_monomial_key_fp == None ) ) :
        xrn.fclose( decompressed_xrn_matrix_fp )
        xrn.fclose( start_point_fp )
        return -1

    if ( xrn.XSUCCESS !=
         xrn.xrn_make_binomial_key_wrapper( decompressed_xrn_matrix_fp, start_point_fp, bob_monomial_key_fp,
                                     bob_binomial_key_fp, settings ) ) :
        xrn.fclose( bob_binomial_key_fp )
        xrn.fclose( decompressed_xrn_matrix_fp )
        xrn.fclose( bob_monomial_key_fp )
        xrn.fclose( start_point_fp )
        return -1

    if ( ( 0 != xrn.fclose( bob_monomial_key_fp ) ) or ( 0 != xrn.fclose( bob_binomial_key_fp ) )
         or ( 0 != xrn.fclose( decompressed_xrn_matrix_fp ) ) or ( 0 != xrn.fclose( start_point_fp ) ) ) :
        return -1

    return 0


def xrn_exchange_monomial_key_wrapper_example ( ):

    ##########################
    ## Variable definitions ##
    ##########################

    # library run time settings

    settings = xrn.xrn_crypto_extra_settings_t ()
    # all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    ##########################
    ## Initialize variables ##
    ##########################

    # Initialize default run time settings

    if ( xrn.XSUCCESS != xrn.xrn_load_default_settings( settings ) ) :
        return -1

    alice_monomial_key_fp = xrn.fopen( "alice_monomial_key.xf", "rb" )
    alice_binomial_key_fp = xrn.fopen( "alice_binomial_key.xf", "rb" )
    bob_binomial_key_fp = xrn.fopen( "bob_binomial_key.xf", "rb" )
    decompressed_xrn_matrix_fp = xrn.fopen( "xrn_matrix.xm", "rb" )
    shared_key_fp = xrn.fopen( "shared_key.xf", "wb" )

    if (( alice_monomial_key_fp == None ) or 
        ( alice_binomial_key_fp == None ) or 
        ( bob_binomial_key_fp == None ) or 
        ( decompressed_xrn_matrix_fp == None ) or 
        ( shared_key_fp == None ) ):
        return -1

    #########################
    ## Generate common key ##
    #########################

    if ( xrn.XSUCCESS != xrn.xrn_exchange_monomial_key_wrapper( decompressed_xrn_matrix_fp,    # Xoron matrix file pointer
                                                       alice_binomial_key_fp,   # Xoron binomial key file pointer
                                                       bob_binomial_key_fp,     # Xoron binomial key file pointer
                                                       alice_monomial_key_fp,   # Xoron monomial key file pointer
                                                       shared_key_fp,   # output monomial key file pointer
                                                       settings    # the run time settings
          ) ) :

        xrn.fclose( alice_binomial_key_fp ) 
        xrn.fclose( alice_monomial_key_fp )
        xrn.fclose( decompressed_xrn_matrix_fp )
        xrn.fclose( bob_binomial_key_fp )
        xrn.fclose( shared_key_fp ) 
        return -1

    elif ( ( 0 != xrn.fclose( alice_binomial_key_fp ) ) or ( 0 != xrn.fclose( alice_monomial_key_fp ) )
             or ( 0 != xrn.fclose( decompressed_xrn_matrix_fp ) ) or ( 0 != xrn.fclose( bob_binomial_key_fp ) )
             or ( 0 != xrn.fclose( shared_key_fp ) ) ) :
            return -1

    # An alternative way to perform the same computation can be achieved using the xrn_exchange_monomial_key function
    # if ( xrn.XSUCCESS != xrn.xrn_exchange_monomial_key( xrn_matrix, # xrn_matrix is of the type ( xrn_matrix_t * )
    #                                   alice_binomial_key, # alice_binomial_key is of the type ( xbinomial_key_t )
    #                                   bob_binomial_key, # bob_binomial_key is of the type ( xbinomial_key_t )
    #                                   alice_monomial_key, # alice_monomial_key is of the type ( xmonomial_key_t )
    #                                   shared_key    # shared_key is of the type ( xmonomial_key_t * )
    #      )):

    return 0


#############
## Logging ##
#############

# Set the default logging file streams
# errors -> stderr
# warnings -> stdout
# notifications -> stdout
# debug -> stdout

xrn.xrn_set_default_log(  )

# To change the default logging you can use the functions below,
# where fp is the opened file pointer
# 
# xrn_change_error_log( fp )
# xrn_change_warning_log( fp )
# xrn_change_notification_log( fp )
# xrn_change_debug_log( fp )

if ( 0 != generate_keys_alice_bob(  ) ) :
    exit ( -1 )

print( "compute common monomial key ... " )
if ( 0 != xrn_exchange_monomial_key_wrapper_example(  ) ) :
    print( "error in key exchange " )
    exit ( -1 )

print( "computed common key " )

exit ( 0 )



